﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习十月份
{
    public class Solution209
    {
        public int MinSubArrayLen1(int target, int[] nums)//自己的思路1
        {
            //滑动窗口
            int sum = 0;
            int len = nums.Length;
            int minLen = len + 1;
            int right = 0;

            for (int left = 0; left < len; left++)
            {
                while (right < len && sum < target)
                {
                    sum += nums[right];
                    right++;
                }

                if (sum >= target)
                {
                    int curLen = right - left;
                    minLen = Math.Min(minLen, curLen);
                    Console.WriteLine(minLen);
                }

                sum -= nums[left];

                Console.WriteLine("left：  " + left + "right：  " + right + "sum：  " + sum);
            }

            if (minLen == len + 1)
            {
                return 0;
            }
            else
            {
                return minLen;
            }
        }

        public int MinSubArrayLen2(int target, int[] nums)//自己的思路2
        {
            //滑动窗口
            int sum = 0;
            int len = nums.Length;
            int minLen = len + 1;
            int right = 0;
            int left = 0;

            while (right < len)
            {
                sum += nums[right++];
                while (sum >= target)
                {
                    int curLen = right - left;
                    minLen = Math.Min(minLen, curLen);
                    Console.WriteLine(minLen);
                    sum -= nums[left++];
                }

                Console.WriteLine("left：  " + left + "right：  " + right + "sum：  " + sum);
            }

            if (minLen == len + 1)
            {
                return 0;
            }
            else
            {
                return minLen;
            }
        }

        public int MinSubArrayLen(int target, int[] nums)//前缀和
        {
            //求的结果是大于等于，不能两数之和

            return target;
        }
    }

    internal class Program209
    {
        private static void Main209(string[] args)
        {
            int target = 7;
            int[] nums = [2, 3, 1, 2, 4, 3];
            Solution209 solution = new Solution209();
            int res = solution.MinSubArrayLen(target, nums);
            Console.WriteLine(res);
        }
    }
}